<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      ref="table"
      columnsRef="partBasedataPartInfoIndex"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <PartModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInfos } from '@/api/part/partInfo';
import PartModal from './partInfoItem';

export default {
  name: 'Part',
  components: {
    PartModal
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createTopFilterList(),
      fetchParams: {},
      visible: false,
      dialogTitle: '',
      fetchApi: queryPartInfos,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '备注...'
        },
        {
          type: 'RANGE_DATE',
          label: '更新日期',
          fieldName: 'updateDate',
          placeholder: '更新日期...'
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          placeholder: '请选择...',
          itemList: [
            { value: 10451001, text: '店面' },
            { value: 10451003, text: '厂家' }
          ]
        },
        {
          type: 'SELECT',
          label: '配件类别',
          fieldName: 'partGroupCode',
          placeholder: '请选择...',
          itemList: [
            { value: 10451001, text: '店面' },
            { value: 10451003, text: '厂家' }
          ]
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'partNo',
          placeholder: '配件代码...'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'partName',
          placeholder: '配件名称...'
        },
        {
          type: 'INPUT',
          label: '配件中缀',
          fieldName: 'partInfix',
          placeholder: '配件中缀...'
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'oem',
          placeholder: '请选择...',
          itemList: this.createDictList(1045)
        },
        {
          type: 'SELECT',
          label: '是否用品',
          fieldName: 'is_acc',
          placeholder: '请选择...',
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '产品组',
          fieldName: 'levelOne_PartInfo',
          placeholder: '请选择...',
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '产品类别',
          fieldName: 'levelTwo_PartInfo',
          placeholder: '请选择...',
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '产品子类',
          fieldName: 'levelThree_PartInfo',
          placeholder: '请选择...',
          itemList: this.createDictList(1004)
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 90,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.showHandle(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'partNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          dataIndex: 'partName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件中缀',
          dataIndex: 'partInfix',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'oemTag',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件库存状态',
          dataIndex: 'partStockStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '常规订货单价',
          dataIndex: 'regularPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '最小包装数',
          dataIndex: 'minPackage',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建时间',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '更新时间',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '用品品牌',
          dataIndex: 'goodsBrands',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '型号',
          dataIndex: 'accMode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所属车型',
          dataIndex: 'partVehicleModel',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建议销售价',
          dataIndex: 'instructPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '急件价格',
          dataIndex: 'urgentPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '替代配件',
          dataIndex: 'optionNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否停用',
          dataIndex: 'partStatus',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否销售',
          dataIndex: 'isCSale',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品组',
          dataIndex: 'productGroupName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品类别',
          dataIndex: 'productTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品子类',
          dataIndex: 'productSubclassName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    showHandle(row) {
      this.handleType = 'show';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}配件基本信息`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
